//
//  MoreButtonScrollView.m
//  KeyBoard
//
//  Created by ShaoFeng on 16/8/18.
//  Copyright © 2016年 Cocav. All rights reserved.
//
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

#define CustomBackGroundColor [UIColor colorWithRed:243 / 255.0 green:243 / 255.0 blue:243 / 255.0 alpha:1]

// 一行中 item的个数
#define ItemCountPerRow 4
// 一页显示多少行
#define RowCount 2
//每页的个数
#define PageCount (ItemCountPerRow * RowCount)

#import "MoreButtonView.h"
#import "UIView+Category.h"

@interface MoreButtonView () <UIScrollViewDelegate>
{
    NSInteger _numberOfPages;
}
@property (nonatomic,strong)UIScrollView *contentScrollView;
@property (nonatomic,strong)UIPageControl *pageControl;

@property (nonatomic,strong)NSArray *itemsContent;

@end

@implementation MoreButtonView


- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {

        self.backgroundColor = CustomBackGroundColor;
        
        //数据源
        self.itemsContent = @[
                              @{@"icon":@"img_defaulthead_nor",@"highIcon":@"img_defaulthead_nor",@"typeTag":@(MoreButtonViewButtonTypeImages),@"title":@"Images"},
                              @{@"icon":@"img_defaulthead_nor",@"highIcon":@"img_defaulthead_nor",@"typeTag":@(MoreButtonViewButtonTypeCamera),@"title":@"Camera"},
                              @{@"icon":@"img_defaulthead_nor",@"highIcon":@"img_defaulthead_nor",@"typeTag":@(MoreButtonViewButtonTypeFile),@"title":@"File"},
                              @{@"icon":@"img_defaulthead_nor",@"highIcon":@"img_defaulthead_nor",@"typeTag":@(MoreButtonViewButtonTypeContact),@"title":@"Contact"},
                              @{@"icon":@"img_defaulthead_nor",@"highIcon":@"img_defaulthead_nor",@"typeTag":@(MoreButtonViewButtonTypeLocation),@"title":@"Location"},
                              @{@"icon":@"img_defaulthead_nor",@"highIcon":@"img_defaulthead_nor",@"typeTag":@(MoreButtonViewButtonTypeSeal),@"title":@"Seal"},
                              @{@"icon":@"img_defaulthead_nor",@"highIcon":@"img_defaulthead_nor",@"typeTag":@(MoreButtonViewButtonTypeEmail),@"title":@"Email"},
                              @{@"icon":@"img_defaulthead_nor",@"highIcon":@"img_defaulthead_nor",@"typeTag":@(MoreButtonViewButtonTypeEmail),@"title":@"Email"},
                              @{@"icon":@"img_defaulthead_nor",@"highIcon":@"img_defaulthead_nor",@"typeTag":@(MoreButtonViewButtonTypeEmail),@"title":@"Email"}
                              ];
        
        _numberOfPages = (self.itemsContent.count % PageCount != 0)? self.itemsContent.count / PageCount + 1 : self.itemsContent.count / PageCount;
        
        [self layoutUI];
    }
    return self;
}

- (void)layoutUI
{
    //scrollView
    self.contentScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height)];
//    self.contentScrollView.backgroundColor = [UIColor cyanColor];
    self.contentScrollView.showsHorizontalScrollIndicator = NO;
    self.contentScrollView.showsVerticalScrollIndicator = NO;
    self.contentScrollView.pagingEnabled = YES;
    self.contentScrollView.bounces = YES;
    self.contentScrollView.delegate = self;
    //设置偏移量
    self.contentScrollView.contentSize = CGSizeMake(self.width * _numberOfPages, self.height);
    [self addSubview:self.contentScrollView];
    
    //pageControl
    self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, self.height - 15, 0, 10)];
    self.pageControl.centerX = self.width * 0.5;
    self.pageControl.currentPageIndicatorTintColor = [UIColor blackColor];
    self.pageControl.pageIndicatorTintColor = [UIColor grayColor];
    self.pageControl.backgroundColor = [UIColor redColor];
    self.pageControl.userInteractionEnabled = NO;
    self.pageControl.numberOfPages = _numberOfPages;
    self.pageControl.hidesForSinglePage = YES;
    [self addSubview:self.pageControl];
    
    //添加子控件
    for (NSDictionary *dic in self.itemsContent)
    {
        NSNumber *typeTag = dic[@"typeTag"];
         [self addButtonWithIcon:dic[@"icon"] highIcon:dic[@"highIcon"] tag:typeTag.intValue title:dic[@"title"]];
    }
}

#pragma mark -- 添加子控件
- (void)addButtonWithIcon:(NSString *)icon highIcon:(NSString *)heighIcon tag:(int )tag title:(NSString *)title
{
    UIButton *button = [[UIButton alloc] init];
    button.tag = tag;
    [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [button setBackgroundImage:[UIImage imageNamed:icon] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:heighIcon] forState:UIControlStateNormal];
    [self.contentScrollView addSubview:button];
    
    UILabel *label = [[UILabel alloc] init];
    label.font = [UIFont systemFontOfSize:14];
    label.text = title;
    label.textAlignment = NSTextAlignmentCenter;
    [self.contentScrollView addSubview:label];
}

//布局子控件
- (void)layoutSubviews
{
    [super layoutSubviews];

    NSMutableArray *mArrayButton = [NSMutableArray array];
    NSMutableArray *mArrayLabel = [NSMutableArray array];
    for (UIView *view in self.contentScrollView.subviews) {
        if ([view isKindOfClass:[UIButton class]]) {
            [mArrayButton addObject:view];
        } else if ([view isKindOfClass:[UILabel class]]) {
            [mArrayLabel addObject:view];
        }
    }
    
    //布局
    CGFloat space = 35;
    CGFloat mixSpace = 15;
    CGFloat topSpace = mixSpace;
    CGFloat buttonW = 55;
    CGFloat buttonH = buttonW;
    
    for (NSInteger i = 0; i < mArrayButton.count; i ++)
    {
        UIButton *button = mArrayButton[i];
        UILabel *label = [mArrayLabel objectAtIndex:i];
        
        double double_Num = i / PageCount;
        int currentPage = floor(double_Num);
        
        //起点X
        CGFloat starX = (self.width -  ((buttonW * ItemCountPerRow) + (space * (ItemCountPerRow - 1)))) / 2;
        if (starX < mixSpace) {
            //重新计算space
            CGFloat temp_Space = (self.width - mixSpace * 2 - (buttonW * ItemCountPerRow)) / (ItemCountPerRow - 1);
            space = floor(temp_Space);
            starX = (self.width -  ((buttonW * ItemCountPerRow) + (space * (ItemCountPerRow - 1)))) / 2;
        }
        starX = starX + self.width * currentPage;
        
        
        CGFloat X = starX + (buttonW + space) * (i % ItemCountPerRow);
        
        CGFloat temp_Num = (currentPage > 0)? (i / ItemCountPerRow) % (currentPage * 2) : (i / ItemCountPerRow);
        CGFloat Y = topSpace  + temp_Num * (buttonH + 35);
        
        button.frame = CGRectMake(X, Y, buttonW, buttonH);
        label.frame = CGRectMake(button.frame.origin.x, CGRectGetMaxY(button.frame) + 3, button.frame.size.width + 5, 20);
    }
}

#pragma mark -- UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    self.pageControl.currentPage = (scrollView.contentOffset.x / self.width);
    
}

- (void)buttonClick:(UIButton *)sender
{
    if ([_delegate respondsToSelector:@selector(moreButtonView:didClickButton:)]) {
        [_delegate moreButtonView:self didClickButton:(MoreButtonViewButtonType)sender.tag];
    }
}

@end
